<?php
	if (!$author_id_field = Configure::read('Lil.userTable.foreignKey')) {
		$author_id_field = 'author_id';
	}
	$post_add = array(
		'title_for_layout' => __d('lil_blogs', 'Add a new Post', true),
		'scripts_for_layout' => array(
			'css' => array(
				'admin' => array(
					'path'    => '/lil_blogs/css/lil_blogs_admin',
					'rel'     => null,
					'options' => array('inline' => true)
				)
			),
			'js' => array(
				// 'name' => array('url' => $script_url, 'options' => $options_array)
			)
		),
		'title' => 	'<div class="head">'.
			'<h1>' . __d('lil_blogs', 'Add a new Post', true) . '</h1>'.
			'</div>',
		'form' => array(
			'pre' => '<div class="form">',
			'post' => '</div>',
			'lines' => array(
				'form_start' => array(
					'class'      => $this->Form,
					'method'     => 'create',
					'parameters' => array('model' => 'Post')
				),
				'id' => array(
					'class'      => $this->Form,
					'method'     => 'hidden',
					'parameters' => array('field' => 'id')
				),
				'blog_id' => array(
					'class'      => $this->Form,
					'method'     => 'hidden',
					'parameters' => array('field' => 'blog_id')
				),
				'referer' => array(
					'class'      => $this->Form,
					'method'     => 'hidden',
					'parameters' => array('field' => 'referer')
				),
				'title' => array(
					'class'      => $this->Form,
					'method'     => 'input',
					'parameters' => array(
						'field'   => 'title',
						'options' => array(
							'label' => __d('lil_blogs', 'Title', true).':',
							'error' => __d('lil_blogs', 'Post title is required.', true),
							'class' => 'big'
						)
					)
				),
				'slug' => (Configure::read('Lil.slug')=='manual') ? null : array(
					'class'      => $this->Form,
					'method'     => 'input',
					'parameters' => array(
						'field'   => 'slug',
						'options' => array(
							'label' => __d('lil_blogs', 'Slug', true).':',
							'error' => __d('lil_blogs', 'Post slug is required and must only use letters, numbers, underscores or hyphens.', true)
						)
					)
				),
				'body' => array(
					'class'      => $this->Form,
					'method'     => 'input',
					'parameters' => array(
						'field'   => 'body',
						'options' => array(
							'label' => __d('lil_blogs', 'Body', true) . ':',
							'rows'  => 12,
							'error' => __d('lil_blogs', 'Body of the post is required.', true)
						)
					)
				),
				'categories' => (Configure::read('Lil.noCategories') || empty($categories)) ? null : array(
					'class'      => $this->Form,
					'method'     => 'input',
					'parameters' => array(
						'field'   => 'Category.Category',
						'options' => array(
							'label'   => __d('lil_blogs', 'Category', true) . ':',
							'options' => $categories,
							'empty'   => '-- ' . __d('lil_blogs', 'none', true) . ' --'
						)
					)
				),
				'author_id' => 
					($this->Lil->isAdmin()) ?
					array(
						'class'      => $this->Form,
						'method'     => 'input',
						'parameters' => array(
							'field'   => $author_id_field,
							'options' => array(
								'label'   => __d('lil_blogs', 'Author', true) . ':',
								'options' => $authors
							)
						)
					)
					:
					array(
						'class'      => $this->Form,
						'method'     => 'input',
						'parameters' => array(
							'field'   => $author_id_field,
							'options' => array(
								'type'    => 'hidden',
								'value' => $this->Lil->user('id')
							)
						)
					),
				'status' => array(
					'class' => $form,
					'method' => 'input',
					'parameters' => array(
						'field'   => 'status',
						'options' => array(
							'label'   => __d('lil_blogs', 'Status', true) . ':',
							'options' => array(
								'0' => __d('lil_blogs', 'Draft', true),
								'2' => __d('lil_blogs', 'Live', true)
							)
						)
					)
				),
				'submit' => array(
					'class'      => $form,
					'method'     => 'submit',
					'parameters' => array(
						'label' => __d('lil_blogs', 'Create', true)
					)
				),
				'form_end' => array(
					'class'      => $form,
					'method'     => 'end',
					'parameters' => array()
				),
			)
		)
	);
	
	$post_add = $this->callPluginHandlers('admin_add_post', $post_add);
	
	$this->set('title_for_layout', $post_add['title_for_layout']);
	
	foreach	($post_add['scripts_for_layout']['css'] as $script_tag => $script_params) {
		echo $this->Html->css($script_params['path'], $script_params['rel'], $script_params['options']);
	}
	
	foreach	($post_add['scripts_for_layout']['js'] as $script_tag => $script_params) {
		echo $this->Html->script($script_params['url'], $script_params['options']);
	}

	// form display begins
	echo $post_add['title']; 
	
	echo $post_add['form']['pre']; 
	foreach ($post_add['form']['lines'] as $name => $line) {
		if (!empty($line['class'])) {
			$parameters = array();
			if (!empty($line['parameters'])) {
				$parameters = (array)$line['parameters'];
			}
			
			if (is_object($line['class'])) {
				$use_object =& $line['class'];
			} else if (is_string($line['class'])) {
				if (substr($line['class'], 0, 6) == 'this->') {
					$func_name = substr($line['class'], 6);
					$use_object =& $this->{$func_name};
				} else {
					$use_object =& ${$line['class']};
				}
			}
			echo call_user_func_array(array($use_object, $line['method']), $parameters);
		}
	}
	echo $post_add['form']['post']; 
?>